package com.zzc.qqchat.controller;

import com.zzc.qqchat.domain.BuildGroupList;
import com.zzc.qqchat.repository.BuildGroupListRep;
import com.zzc.qqchat.domain.Group;
import com.zzc.qqchat.repository.GroupRep;
import com.zzc.qqchat.domain.Message;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.sql.Time;
import java.util.ArrayList;
import java.util.List;

@Controller
public class BuildGroupController {

    @Autowired
    private SimpMessagingTemplate simpMessagingTemplate;

    @Resource
    GroupRep groupRep;

    @Resource
    BuildGroupListRep buildGroupListRep;



    @ResponseBody
    @PostMapping("/buildgroup")
    public int buildgroup(int userId,String userName,String groupName,String groupPicture,String[] receId){

        Group group = new Group();

        List<Group> g = groupRep.findAll();
        group.setGroupId(g.size());
        group.setGroupName(groupName);
        group.setGroupPicture(groupPicture);
        group.setUserId(userId);
        groupRep.save(group);
        Time time = new Time(0);
        for(int i=0;i<receId.length;i++){
            BuildGroupList buildGroupList = new BuildGroupList();
            Message message = new Message(String.valueOf(g.size()),userId,Integer.parseInt(receId[i]),time,userName,"buildgroup");
            simpMessagingTemplate.convertAndSendToUser(receId[i],"/message/get",message);
            List<BuildGroupList> buildGroupLists = buildGroupListRep.findAll();
            buildGroupList.setId(buildGroupLists.size());
            buildGroupList.setGroupId(g.size());
            buildGroupList.setReceId(Integer.parseInt(receId[i]));
            buildGroupList.setSenderId(userId);
            buildGroupList.setSenderName(userName);
            buildGroupListRep.save(buildGroupList);
        }
        return g.size();
    }

    @ResponseBody
    @PostMapping("/buildlist")
    public ArrayList<BuildGroupList> buildList(int userId){
        ArrayList<BuildGroupList> buildGroupLists = buildGroupListRep.findByReceId(userId);
        return buildGroupLists;
    }

    @ResponseBody
    @PostMapping("/accessinvite")
    public void accessInvite(int userId,int groupId){
        ArrayList<Group> groups = groupRep.findByGroupId(groupId);
        Group group = new Group();
        group.setUserId(userId);
        group.setGroupPicture(groups.get(0).getGroupPicture());
        group.setGroupName(groups.get(0).getGroupName());
        groupRep.save(group);
        Time time = new Time(0);
        Message message = new Message(groups.get(0).getGroupPicture(),groupId,userId,time,groups.get(0).getGroupName(),"accessgroup");
        simpMessagingTemplate.convertAndSendToUser(String.valueOf(userId),"/message/get",message);
        buildGroupListRep.deleteByReceAndSenderId(userId,groupId);
    }

    @ResponseBody
    @PostMapping("/refuseinvite")
    public void refuseInvite(int userId,int groupId,String userName){
        ArrayList<BuildGroupList> buildGroupLists = buildGroupListRep.findByReceIdAndGroupId(userId,groupId);
        Time time = new Time(0);
        Message message = new Message("",userId,buildGroupLists.get(0).getSenderId(),time,userName,"refuseinvite");
        simpMessagingTemplate.convertAndSendToUser(String.valueOf(buildGroupLists.get(0).getSenderId()),"/message/get",message);
        buildGroupListRep.deleteByReceAndSenderId(userId,groupId);
    }

    @ResponseBody
    @PostMapping("/grouplist")
    public ArrayList<Group> groupList(int userId){
        ArrayList<Group> groups = groupRep.findByUserId(userId);
        return groups;
    }
}
